<template>
    <div class="table-page">

        <div class="table-search-box" @keyup.enter="search()">
            <div class="search-item">
                <label>经度：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.longitude"
                        type="number"
                        clearable
                        placeholder="经度"
                    ></el-input>
                </div>
            </div>
            <div class="search-item">
                <label>纬度：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.latitude"
                        type="number"
                        clearable
                        placeholder="纬度"
                    ></el-input>
                </div>
            </div>
            <div class="search-item">
                <label>机构名称：</label>
                <div class="table-query-input">
                    <el-input
                        v-model="queryData.groupName"
                        clearable
                        placeholder="机构名称"
                    ></el-input>
                </div>
            </div>
            <div class="search-item">
                <label>绑定时间：</label>
                <div class="table-query-input">
                    <el-date-picker
                        v-model="queryData.bindDay"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择绑定时间">
                    </el-date-picker>
                </div>
            </div>
            <div class="search-item">
                <label>到期时间：</label>
                <div class="table-query-input">
                    <el-date-picker
                        v-model="queryData.expireTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择到期时间">
                    </el-date-picker>
                </div>
            </div>
            <div class="search-item">
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    :loading="tableLoading"
                    @click="clickSearch()"
                >搜索
                </el-button>
            </div>
            <div class="search-item">
                <el-button
                    icon="el-icon-refresh"
                    :loading="tableLoading"
                    @click="resetSearch()"
                >重置
                </el-button>
            </div>
        </div>
        <div class="table-data-box">
            <el-table
                ref="table"
                v-loading="tableLoading"
                :data="tableData"
                border
                style="width: 100%;min-height:40em"
                row-key="id"
            >
                <el-table-column
                    prop="longitude"
                    label="经度"
                ></el-table-column>
                <el-table-column
                    prop="latitude"
                    label="纬度"
                ></el-table-column>
                <el-table-column
                    prop="remark"
                    label="备注"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="groupName"
                    label="绑定机构"
                ></el-table-column>
                <el-table-column
                    prop="bindTime"
                    label="绑定时间"
                ></el-table-column>
                <el-table-column
                    prop="expireTime"
                    label="到期时间"
                ></el-table-column>
                <el-table-column
                    prop="bindUserName"
                    label="操作者"
                ></el-table-column>
                <el-table-column
                    label="操作"
                    min-width="200"
                    align="center"
                    fixed="right"
                >
                    <template slot-scope="scope">
                        <el-button
                            v-if="!scope.row.edit"
                            lass="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-edit"
                            :loading="scope.row.opLoading"
                            @click="
                                clickSimpleEdit(scope.row, '修改机构位置')
                            "
                        >修改
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            lass="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-delete"
                            :loading="scope.row.opLoading"
                            @click="
                                tableDelete(
                                    '/user/sys/group/location/delete',
                                    {
                                        id: scope.row.id
                                    },
                                    true
                                )
                            "
                        >删除
                        </el-button>
                        <el-button
                            v-if="!scope.row.groupId"
                            lass="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-edit"
                            :loading="scope.row.opLoading"
                            @click="
                               toReBind(scope.row, '关联机构')
                            "
                        >关联机构
                        </el-button>
                        <el-button
                            v-if="scope.row.groupId"
                            lass="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-edit"
                            :loading="scope.row.opLoading"
                            @click="unBind(scope.row)"
                        >取消机构关联
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-if="pageData.total" class="xy-between table-page-box">
            <el-pagination
                background
                :current-page.sync="pageData.currentPage"
                :page-sizes="pageData.pageSizes"
                :page-size.sync="pageData.pageSize"
                layout="total,  prev, pager, next, sizes, jumper"
                :total="pageData.total"
            >
            </el-pagination>
        </div>
        <el-dialog
            :title="dialogFormTitle"
            :visible.sync="dialogFormVisible"
            :before-close="dialogClose"
            :modal="false"
            custom-class="two-dialog"

        >
            <el-form
                ref="dataForm"
                :model="dataForm"
                label-width="8em"
                :rules="dataFormRule"

            >
                <el-form-item label="经度" prop="longitude">
                    <el-input v-model="dataForm.longitude"></el-input>
                </el-form-item>
                <el-form-item label="纬度" prop="latitude">
                    <el-input v-model="dataForm.latitude"></el-input>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="dataForm.remark" type="textarea" rows="5"></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button :loading="submitFlag" @click="dialogClose"
                >取 消</el-button
                >
                <el-button
                    :loading="submitFlag"
                    type="primary"
                    @click="formDataSave('/user/sys/group/location/update')"
                >保 存</el-button
                >
            </span>
        </el-dialog>
        <el-dialog :title="reBindTitle" :visible.sync="reBindVisible" width="80%" :modal="false">
            <div class="el-dialog-div">
                <group-bind-component
                    v-if="reBindVisible"
                    :group-ip-id="reBindId"
                    @bind="closeGroup"
                >
                </group-bind-component>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import {TablePage} from "@/layout/mixin/iVue.js";
import {mapState} from "vuex";
import {unBindLocation, reBindLocation} from "@/api/user";
import groupBindComponent from "@/views/models/vrs/group/groupBind";
import {post} from "@/api/request";
import {checkLat, checkLong} from "@/utils/validate";

export default {
    name: "GroupIpAll",
    components: {groupBindComponent},
    mixins: [TablePage],
    props: {
        groupId: {
            type: Number,
            default: null
        }
    },
    data() {
        return {
            queryData: {
                groupName:null,
                longitude:null,
                latitude:null,
                expireTime:null,
                bindDay: null
            },
            initAddForm: {
                startIp: null,
                endIp: null,
                remark: null,
                groupId: this.groupId,
            },
            dataFormRule: {
                longitude: [
                    {
                        required: true,
                        message: "请输入经度",
                        trigger: "blur"
                    },
                    {
                        validator : (rule,value,callback) =>{
                            if (value){
                                if (checkLong(value)){
                                    callback();
                                }else {
                                    callback(new Error("经度整数部分为0-180,小数部分为0到6位!"))
                                }
                            }
                        },
                        trigger: "blur"
                    }

                ],
                latitude: [
                    {
                        required: true,
                        message: "请输入纬度",
                        trigger: "blur"
                    },
                    {
                        validator : (rule,value,callback) =>{
                            if (value){
                                if (checkLat(value)){
                                    callback();
                                }else {
                                    callback(new Error("纬度整数部分为0-90,小数部分为0到6位!"))
                                }
                            }
                        },
                        trigger: "blur"
                    }

                ],
            },
            groupIpDetailFormTitle: null,
            groupIpDetailFormVisible: false,
            groupIpDetailGroupIpId: null,
            reBindVisible: null,
            reBindTitle: null,
            reBindId: null,
            batchDialogFormTitle: null,
            batchDialogFormVisible: false,
        };
    },
    computed: {
        ...mapState({})
    },
    watch: {},
    created() {
    },
    mounted() {
    },
    methods: {
        search(queryData) {
            this.loadTable("/user/sys/group/location/page", queryData, true,);
        },
        unBind(data) {
            unBindLocation({id: data.id}).then(res => {
                this.search();
            })
        },
        toReBind(data, title) {
            this.reBindVisible = true;
            this.reBindTitle = title;
            this.reBindId = data.id;
        },
        closeGroup(groupId) {
            reBindLocation({id: this.reBindId, groupId: groupId}).then(res => {
                this.search();
                this.$message.success("绑定成功")
                this.reBindVisible = false;

            })
        }
    }
};
</script>

<style lang="scss" scoped>
.school-logo {
    .logo-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 108px;
        height: 108px;
        line-height: 108px;
        text-align: center;
    }

    .logo-img {
        width: 108px;
        height: 108px;
        display: block;
    }
}

.table-edit-from {
    .address-select {
        width: 8em;
        margin-right: 2em;
    }
}
</style>
<style>
.school-logo .logo-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.school-logo .logo-uploader .el-upload:hover {
    border-color: #409eff;
}
</style>
